home *** CD-ROM | disk | FTP | other *** search
/ Commodore Free 8 / Commodore_Free_Issue_08_2007_Commodore_Computer_Club.d64 / t.basic bible < prev    next >
Text File  |  2023-02-26  |  9KB  |  376 lines

  1. uTHE BASIC BIBLE
  2. By David Moorman
  3.  
  4. Here is an alphabetical list of BASIC
  5. 2.0 commands, functions, & operators.
  6. Browse through them. Try them out.
  7. Learn exactly what they do! Commands
  8. (com) tell the computer what to do.
  9. Functions (fun) perform magic on values
  10. or strings in the argument (within the
  11. parentheses), & must be used either in
  12. a PRINT command or as  the object of a
  13. variable assignment. For example...
  14.  
  15. 5 A = -1234
  16. 10 ?ABS(A)
  17. 20 V = ABS(A)
  18.  
  19. ABS(n) (fun)
  20. Returns the positive value of the
  21. argument, whether it is positive or
  22. negative.
  23.  
  24. 10 A = ABS(-123)
  25.   (A will contain the value of 123)
  26.  
  27. ASC(string) (fun)
  28. Returns the ASCII value of a string
  29. character. If the string has more than
  30. one character, only the first
  31. character's value is returned.
  32.  
  33. 10 A = ASC("A")
  34.   (A will contain 65)
  35.  
  36. ATN(n) (fun)
  37. Returns the arctangent of argument.
  38.  
  39. 10 A = ATN(1)
  40.   (A will contain 0.785398163
  41.   arctangent of 1 in radians)
  42.  
  43. CHR$(n) (fun)
  44. Returns a string character for the
  45. ASCII value in the argument.
  46.  
  47. 10 A$ = CHR$(65)
  48.   (A$ will contain an "A".)
  49.  
  50. CLOSE (com)
  51. Closes logic file. See OPEN for
  52. details.
  53.  
  54. CLR (com)
  55. Sets all variables in a program to zero
  56. or null (empty strings). Use at the top
  57. of your program.
  58.  
  59. CMD lf (com)
  60. CoMmand command. See OPEN for details
  61.  
  62. CONT (com)
  63. CONTinue -- use in Immediate Mode to
  64. continue running a program. Will not
  65. work after an error or after editing
  66. the program.
  67.  
  68. COS(n) (fun)
  69. Returns the Cosine of the argument.
  70.  
  71. 10 A = COS(1)
  72.   (A will contain 0.540302306 - cosine
  73.  of 1 in radians)
  74.  
  75. DATA (com)
  76. Used with READ to load string or
  77. numeric data into variables or arrays.
  78. See READ for details.
  79.  
  80.  
  81. DEF FNv(V)(com)
  82. DEFines a FuNction. Need a complex math
  83. function several times in a program?
  84. Here is where you can create your own
  85. functions. The function's name always
  86. begins with FN, plus a single letter to
  87. identify it, followed by a variable
  88. within parentheses. This variable is
  89. used in the following math as whatever
  90. value you will later put in your
  91. FuNction. DEFine FuNctions early in
  92. your program.
  93.  
  94. 10 DEF FNH(X)= INT(X/256)
  95. 20 DEF FNL(X)= X-FNH(X)*256
  96.  (These functions will divide a value
  97.  into High & Low byte values -- used to
  98.  address memory with some ML routines.)
  99. 30 A = 49152
  100. 40 ? FNH(A),FNL(A)
  101.  (The computer will print: 192 & 0)
  102.  
  103. DIM v [,v [,v [etc]]] (com)
  104. DIM DIMensions space for Arrays, & is
  105. required for arrays with more than 11
  106. elements. You can also use DIM to tell
  107. the program which variables you are
  108. going to use. This puts the listed
  109. variables at the beginning of variable
  110. memory, so they are easier to find.
  111.  
  112. 10 DIM A(12),X,Y,DT$(2,15)
  113.  
  114. END (com)
  115. Ends the program.
  116.  
  117. EXP(n) (fun)
  118. Returns the constant "e" (a math value)
  119. raised to the power of the argument.
  120.  
  121. 10 ? EXP(1)
  122.   (This will print 2.71828183)
  123.  
  124. FOR v TO v [step v] (com)
  125.   Begins a FOR-NEXT loop.
  126.  
  127. 10 FOR X = 1 TO 10
  128. 20 ? X
  129. 30 NEXT
  130.  
  131. FRE(0) (fun)
  132. Returns number of bytes available for
  133. BASIC program. The argument is a dummy.
  134. NOTE: If the value is greater than
  135. 32767, a negative value is returned
  136. that must be added to 65535.
  137.  
  138. 10 DEF FNF(x)= -(FRE(0)<0)*65535+FRE(0)
  139.  (This custom function will return free
  140. memory as a positive value.)
  141. 20 PRINT FNF(FRE(0))
  142.  
  143. GET (com)
  144. Gets a keystroke into the variable that
  145. follows. A numeric variable is valid,
  146. but the program will crash if an alpha
  147. key is pressed. Use a string variable.
  148. GET just grabs one keystroke, if it is
  149. there. Use something like the following
  150. to grab keystrokes:
  151.  
  152. 10 GET Z$: IF Z$ = "" THEN 10
  153. 20 ?"YOU JUST PRESSED <" Z$ ">"
  154. 30 ?"TRY AGAIN? (Y/N)";
  155. 40 GET Z$: IF Z$ = "" THEN 40
  156. 50 ?Z$
  157. 60 IF Z$ = "Y" THEN 10
  158. 70 END
  159.  
  160. GOSUB linenumber (com)
  161. Jumps program to a subroutine at the
  162. given line number. Program will return
  163. to after the GOSUB command when it
  164. encounters a RETURN command.
  165.  
  166. GOTO linenumber (com)
  167. Jumps program to the given line number.
  168.  
  169. IF <compare is true> THEN... When two
  170. values or strings are compared, the
  171. result is either -1 (for true) or 0
  172. (for false). In an IF-THEN command, if
  173. the value after IF is not zero
  174. (therefore true), the code after THEN
  175. is executed. If the value is 0, the
  176. program "falls through" to the next
  177. line. The code after THEN can be a line
  178. number to jump to, or more commands.
  179.  
  180. 10 IF A=B THEN 1000
  181. 20 IF B=C THEN A = B: GOTO 10
  182.  
  183. INPUT (com)
  184. INPUT allows the user to type in
  185. info in a program. Here are some ways
  186. to use INPUT.
  187.  
  188. 10 INPUT"WHAT IS YOUR NAME";N$
  189. 20 ?"HELLO, "N$"!"
  190. 30 ?"WHAT IS YOUR AGE (NUMBER ONLY)
  191. 40 INPUT AGE
  192. 50 ?"YOU WILL BE 100 IN"100-AGE"YEARS."
  193. 60 INPUT"PICK TWO NUMBERS, SEPARATED BY
  194.     A COMMA";A,B
  195. 70 ?"YOU PICKED:"A"AND"B"."
  196.  
  197. INPUT is an old command & can cause
  198. many problems. An input of a string
  199. when a value is expected will result in
  200. an error. If a comma is included when
  201. inputing a single variable string, it
  202. is interpreted as two string inputs, &
  203. everything from the comma on is lost.
  204. Also, the user can accidentally press a
  205. cursor key & move off the INPUT line,
  206. screwing everything up.
  207.  
  208. Therefore, use INPUT only as you are
  209. learning -- & always get a string
  210. variable. You can convert a string to a
  211. value with the VAL(string) function.
  212. Later, when you start to use a toolbox
  213. module or DotBASIC, you will have
  214. alternative INPUT commands available
  215. that work more dependably. With a
  216. little ingenuity, you can build an
  217. INPUT subroutine, using the GET command
  218. to get keystrokes & put them together
  219. in a string variable. Sounds like a
  220. good project, eh?
  221.  
  222. INPUT#lf,v
  223. Inputs a value or string from a disk
  224. file. See OPEN for more information.
  225.  
  226. INT(n) (fun)
  227. Returns the next smaller whole value of
  228. the argument.
  229.  
  230. 10 A = 123.456
  231. 20 B = -123.456
  232. 30 ? INT(A)
  233. 40 ? INT(B)
  234. (The values printed will be 123 & -122.
  235. We did say "next smaller!")
  236.  
  237. LEFT$(s,n) (fun)
  238. Returns a string from string S that is
  239. N characters long.
  240.  
  241. 10 A$ = "THIS IS A TEST"
  242. 20 B$ = LEFT$(A$,4)
  243.   (B$ will contain "THIS".)
  244.  
  245. LEN(s) (fun)
  246. Returns the length of string S.
  247.  
  248. 10 A$="THIS IS A TEST"
  249. 20 ?LEN(A$)
  250.   (Prints 14.)
  251.  
  252. LET (com)
  253. The old fashioned way to assign a
  254. variable.
  255.  
  256. 10 LET A = 12
  257. 20 LET B$ = "TEST"
  258.  
  259.   This is not used anymore. Just put
  260. the variable first, like we have done
  261. throughout this book!
  262.  
  263. 10 A = 12
  264. 20 B$ = "TEST"
  265. LIST (com)
  266. Lists the BASIC program in memory. Used
  267. in Immediate Mode.
  268.  
  269. LIST      > lists whole program
  270. LIST10    > lists line 10 (if present)
  271. LIST10-   > lists line 10 & on.
  272. LIST-10   > lists everything to &
  273.  including line 10
  274. LIST10-20 > lists lines 10 & 20 &
  275.  everything in between.
  276.  
  277. You can slow down a LIST by holding
  278. down the <CONTROL> key (VICE: <Tab>), &
  279. stop it by pressing <STOP> (VICE:
  280.  <ESC>).
  281.  
  282.  
  283. LOAD "filename",dv [,sa] (com)
  284. Loads a program or file into memory
  285. from a disk. If SA is 1, the file is
  286. loaded to the memory location were it
  287. was saved. LOAD is usually used from
  288. Immediate Mode, but can be used in a
  289. program -- with certain caveats. If one
  290. tries to load a BASIC program from
  291. within another program, the loaded
  292. program must be either shorter than the
  293. loading program, or must have a CLR as
  294. its first command.
  295.  
  296. The loaded program will be immediately
  297. RUN. If LOAD is used to load a binary
  298. file, the program will jump back to the
  299. beginning & start over. We use other
  300. routines to load binary (data) files.
  301. See SECRETS later in this book.
  302.  
  303. LOG(v) (fun)
  304. Returns natural logarithm of argument.
  305. Argument must be greater than or equal
  306. to zero.
  307.  
  308. 10 V = 7
  309. 20 PWR = LOG(V)/LOG(2)
  310. 30 ? 2^PWR
  311.  
  312.  This code uses LOG to find the power
  313. of 2 of the value 7. 2 to the PWR will
  314. equal 7!
  315.  
  316. MID$(s,b [,l]) (fun)
  317.  
  318.  Returns the section of string S
  319. beginning at position B to the end for
  320. the length of L.
  321.  
  322. 10 A$ = "THIS IS A TEST"
  323. 20 B$ = MID$(A$,6)
  324. 30 C$ = MID$(A$,9,1)
  325.   (B$ will contain "IS A TEST". C$ will
  326.  contain "A".)
  327.  
  328. NEW (com)
  329. Erases BASIC program memory. Careful!
  330.  
  331. NEXT (com)
  332. See FOR-NEXT above.
  333.  
  334. ON v GOSUB ln [,ln [,ln [etc]]]
  335. Value V determines which line number
  336. GOSUB gosubs.
  337.  
  338. 10 DIM C$(3)
  339. 20 FOR X = 1 TO 3: READ C$(X):NEXT
  340. 30 DATA "FIRE","RAM SPEED","RUN AWAY"
  341. 100 ?"CAPTIAN - WHAT SHALL WE DO?"
  342. 101 FOR X = 1 TO 3:?C$(X):NEXT
  343. 110 INPUT C$
  344. 120 Y = 0: FOR X = 1 TO 3
  345. 130 IF C$ = C$(X) THEN Y = X: X = 3
  346. 140 NEXT
  347. 150 ON Y GOSUB 1000, 2000, 3000
  348. 160 GOTO 100
  349. 1000 ?"FIRE"
  350. 1010 RETURN
  351. 2000 ?"RAM SPEED"
  352. 2010 RETURN
  353. 3000 ?"RUN AWAY"
  354. 3010 RETURN
  355.  
  356. This has got to be the shortest Star
  357. Trek game ever written! You can take it
  358. from here! If you have too many line
  359. numbers to fit on two screen lines, do
  360. something like this:
  361.  
  362. 200 ON X GOSUB 1000,2000,3000
  363. 201 IF X < 4 THEN 100
  364. 202 ON X-3 GOSUB4000,5000,6000
  365. 203 IF X < 7 THEN 100
  366. 204 ON X-6 GOSUB 7000,8000,9000
  367. 205 GOTO 100
  368.  
  369. If the value is 0 or greater than the
  370. number of available line numbers, the
  371. program falls through. Value cannot be
  372. negative.
  373.  
  374. ON n GOTO ln [,ln [,ln [etc]]] (com)
  375. Just like ON-GOSUB, but with no RETURN.
  376.